{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Advanced Plotting With Partial Dependence\n\nThe :func:`~sklearn.inspection.plot_partial_dependence` function returns a\n:class:`~sklearn.inspection.PartialDependenceDisplay` object that can be used\nfor plotting without needing to recalculate the partial dependence. In this\nexample, we show how to plot partial dependence plots and how to quickly\ncustomize the plot with the visualization API.\n\n<div class=\"alert alert-info\"><h4>Note</h4><p>See also `sphx_glr_auto_examples_plot_roc_curve_visualization_api.py`</p></div>\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(__doc__)\n\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom sklearn.datasets import load_boston\nfrom sklearn.neural_network import MLPRegressor\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.tree import DecisionTreeRegressor\nfrom sklearn.inspection import plot_partial_dependence"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Train models on the boston housing price dataset\n================================================\n\nFirst, we train a decision tree and a multi-layer perceptron on the boston\nhousing price dataset.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "boston = load_boston()\nX = pd.DataFrame(boston.data, columns=boston.feature_names)\ny = boston.target\n\ntree = DecisionTreeRegressor()\nmlp = make_pipeline(StandardScaler(),\n                    MLPRegressor(hidden_layer_sizes=(100, 100),\n                                 tol=1e-2, max_iter=500, random_state=0))\ntree.fit(X, y)\nmlp.fit(X, y)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plotting partial dependence for two features\n============================================\n\nWe plot partial dependence curves for features \"LSTAT\" and \"RM\" for\nthe decision tree. With two features,\n:func:`~sklearn.inspection.plot_partial_dependence` expects to plot two\ncurves. Here the plot function place a grid of two plots using the space\ndefined by `ax` .\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig, ax = plt.subplots(figsize=(12, 6))\nax.set_title(\"Decision Tree\")\ntree_disp = plot_partial_dependence(tree, X, [\"LSTAT\", \"RM\"], ax=ax)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The partial depdendence curves can be plotted for the multi-layer perceptron.\nIn this case, `line_kw` is passed to\n:func:`~sklearn.inspection.plot_partial_dependence` to change the color of\nthe curve.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig, ax = plt.subplots(figsize=(12, 6))\nax.set_title(\"Multi-layer Perceptron\")\nmlp_disp = plot_partial_dependence(mlp, X, [\"LSTAT\", \"RM\"], ax=ax,\n                                   line_kw={\"c\": \"red\"})"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plotting partial dependence of the two models together\n======================================================\n\nThe `tree_disp` and `mlp_disp`\n:class:`~sklearn.inspection.PartialDependenceDisplay` objects contain all the\ncomputed information needed to recreate the partial dependence curves. This\nmeans we can easily create additional plots without needing to recompute the\ncurves.\n\nOne way to plot the curves is to place them in the same figure, with the\ncurves of each model on each row. First, we create a figure with two axes\nwithin two rows and one column. The two axes are passed to the\n:func:`~sklearn.inspection.PartialDependenceDisplay.plot` functions of\n`tree_disp` and `mlp_disp`. The given axes will be used by the plotting\nfunction to draw the partial dependence. The resulting plot places the\ndecision tree partial dependence curves in the first row of the\nmulti-layer perceptron in the second row.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 10))\ntree_disp.plot(ax=ax1)\nax1.set_title(\"Decision Tree\")\nmlp_disp.plot(ax=ax2, line_kw={\"c\": \"red\"})\nax2.set_title(\"Multi-layer Perceptron\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Another way to compare the curves is to plot them on top of each other. Here,\nwe create a figure with one row and two columns. The axes are passed into the\n:func:`~sklearn.inspection.PartialDependenceDisplay.plot` function as a list,\nwhich will plot the partial dependence curves of each model on the same axes.\nThe length of the axes list must be equal to the number of plots drawn.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Sets this image as the thumbnail for sphinx gallery\n# sphinx_gallery_thumbnail_number = 4\nfig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 6))\ntree_disp.plot(ax=[ax1, ax2], line_kw={\"label\": \"Decision Tree\"})\nmlp_disp.plot(ax=[ax1, ax2], line_kw={\"label\": \"Multi-layer Perceptron\",\n                                      \"c\": \"red\"})\nax1.legend()\nax2.legend()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "`tree_disp.axes_` is a numpy array container the axes used to draw the\npartial dependence plots. This can be passed to `mlp_disp` to have the same\naffect of drawing the plots on top of each other. Furthermore, the\n`mlp_disp.figure_` stores the figure, which allows for resizing the figure\nafter calling `plot`. In this case `tree_disp.axes_` has two dimensions, thus\n`plot` will only show the y label and y ticks on the left most plot.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "tree_disp.plot(line_kw={\"label\": \"Decision Tree\"})\nmlp_disp.plot(line_kw={\"label\": \"Multi-layer Perceptron\", \"c\": \"red\"},\n              ax=tree_disp.axes_)\ntree_disp.figure_.set_size_inches(10, 6)\ntree_disp.axes_[0, 0].legend()\ntree_disp.axes_[0, 1].legend()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plotting partial dependence for one feature\n===========================================\n\nHere, we plot the partial dependence curves for a single feature, \"LSTAT\", on\nthe same axes. In this case, `tree_disp.axes_` is passed into the second\nplot function.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "tree_disp = plot_partial_dependence(tree, X, [\"LSTAT\"])\nmlp_disp = plot_partial_dependence(mlp, X, [\"LSTAT\"],\n                                   ax=tree_disp.axes_, line_kw={\"c\": \"red\"})"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}